package net.jonbuck.tassoo.ui.view.page;

import java.util.Formatter;

import net.jonbuck.tassoo.model.Task;
import net.jonbuck.tassoo.ui.nl.Messages;
import net.jonbuck.tassoo.ui.util.CommonImages;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.part.IPageSite;
import org.eclipse.ui.part.Page;
import org.eclipse.wb.swt.SWTResourceManager;

/**
 * 
 * 
 * @since 1.0.0
 */
public class TaskViewPage extends Page implements ISelectionListener, IPartListener {

	/** ... TODO Javadoc ... */
	private CTabItem attachments;

	/** ... TODO Javadoc ... */
	private CTabItem comments;

	/** ... TODO Javadoc ... */
	Composite container;

	/** ... TODO Javadoc ... */
	private CTabFolder folder;

	/** ... TODO Javadoc ... */
	private Form form;

	/** ... TODO Javadoc ... */
	private CTabItem general;

	/** ... TODO Javadoc ... */
	private CTabItem recurrence;

	/** ... TODO Javadoc ... */
	private CTabItem reminder;

	/** ... TODO Javadoc ... */
	private Task task;

	/** ... TODO Javadoc ... */
	private FormToolkit toolkit;

	private Composite noDataComposite;

	private GridData gd_NoDataComposite;

	private GridData gd_Folder;

	private Composite bodyComposite;

	/**
	 * 
	 */
	public void createControl(Composite parent) {
		toolkit = new FormToolkit(parent.getDisplay());
		form = toolkit.createForm(parent);
		form.setLayout(new GridLayout());
		form.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		form.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		form.setBackgroundMode(SWT.INHERIT_FORCE);
		form.setText(StringUtils.EMPTY);
		
		toolkit.decorateFormHeading(form);
		
		bodyComposite = form.getBody();
		bodyComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		bodyComposite.setBackgroundMode(SWT.INHERIT_FORCE);
		GridLayout gl_bodyComposite = new GridLayout();
		gl_bodyComposite.horizontalSpacing = 0;
		gl_bodyComposite.marginBottom = 0;
		gl_bodyComposite.marginHeight = 0;
		gl_bodyComposite.marginLeft = 0;
		gl_bodyComposite.marginRight = 0;
		gl_bodyComposite.marginTop = 0;
		gl_bodyComposite.marginWidth = 0;
		gl_bodyComposite.verticalSpacing = 0;
		bodyComposite.setLayout(gl_bodyComposite);
		bodyComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		noDataComposite = new Composite(bodyComposite, SWT.NONE);
		noDataComposite.setLayout(new GridLayout());
		gd_NoDataComposite = new GridData(SWT.FILL, SWT.FILL, true, true);
		noDataComposite.setLayoutData(gd_NoDataComposite);
		
		Label lblNoData = new Label(noDataComposite, SWT.NONE);
		lblNoData.setText("There is currently no selected task to display");
		
		folder = new CTabFolder(bodyComposite, SWT.BOTTOM);
		gd_Folder = new GridData(SWT.FILL, SWT.FILL, true, true);
		folder.setLayoutData(gd_Folder);

		general = new CTabItem(folder, SWT.NONE, 0);
		general.setText("General");
		general.setImage(CommonImages.getImage(CommonImages.PAGE_GREEN));
		general.setControl(new TaskGeneralTab(folder, form, 0));

		comments = new CTabItem(folder, SWT.NONE, 1);
		comments.setText("Comments");
		comments.setImage(CommonImages.getImage(CommonImages.NOTE));
		comments.setControl(new TaskCommentsTab(folder, form, 1));

		attachments = new CTabItem(folder, SWT.NONE, 2);
		attachments.setText("Attachments");
		attachments.setControl(new TaskAttachmentsTab(folder, form, 2));

		recurrence = new CTabItem(folder, SWT.NONE, 3);
		recurrence.setText("Recurrence");
		recurrence.setControl(new TaskRecurrenceTab(folder, form, 3));

		reminder = new CTabItem(folder, SWT.NONE, 4);
		reminder.setText("Reminder");

		folder.setSelection(general);

		if (task == null) {
			gd_Folder.exclude = true;
			folder.setVisible(false);
			bodyComposite.layout();
		}
				
	}

	/**
	 * 
	 */
	public Control getControl() {
		return form;
	}

	/**
	 * 
	 */
	public void init(IPageSite pageSite) {
		super.init(pageSite);
		pageSite.getPage().addPartListener(this);

	}

	/**
	 * 
	 */
	public void partActivated(IWorkbenchPart part) {
		part.getSite().getPage().addSelectionListener(this);

	}

	/**
	 * 
	 */
	public void partBroughtToTop(IWorkbenchPart part) {
	}

	/**
	 * 
	 */
	public void partClosed(IWorkbenchPart part) {
		part.getSite().getPage().removeSelectionListener(this);

	}

	/**
	 * 
	 */
	public void partDeactivated(IWorkbenchPart part) {
		part.getSite().getPage().removeSelectionListener(this);
	}

	/**
	 * 
	 */
	public void partOpened(IWorkbenchPart part) {
		part.getSite().getPage().addSelectionListener(this);
	}

	/**
	 * 
	 */
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		IStructuredSelection selected = (IStructuredSelection) selection;
		Object selectedObject = selected.getFirstElement();
		if (selectedObject instanceof Task) {
			task = (Task) selectedObject;
			form.setText(Messages.bind(Messages.propertiesview_task_title, new Formatter().format("%05d", task.getId()),
					task.getTaskName()));
			((TaskGeneralTab) general.getControl()).setInput(task);
			((TaskCommentsTab) comments.getControl()).setInput(task);
			((TaskAttachmentsTab) attachments.getControl()).setInput(task);
			((TaskRecurrenceTab) recurrence.getControl()).setInput(task);
			
			gd_Folder.exclude = false;
			folder.setVisible(true);
			gd_NoDataComposite.exclude = true;
			noDataComposite.setVisible(false);
			bodyComposite.layout();
		}
	}

	/**
	 * 
	 */
	public void setFocus() {
	}

}
